package com.hxd.otc.module.convenience.model;

import android.support.annotation.NonNull;

import com.hxd.otc.api.IConvenienceApi;
import com.hxd.otc.base.BaseBean;
import com.hxd.otc.module.convenience.bean.ChooseHydropowerCityBean;
import com.hxd.otc.module.convenience.bean.ConvenienceBannerBean;
import com.hxd.otc.module.convenience.bean.ConvenienceOrderBean;
import com.hxd.otc.module.convenience.bean.ConvenienveHistoryBean;
import com.hxd.otc.module.convenience.bean.NorGoodsBean;
import com.hxd.otc.module.convenience.bean.OrderFinishedBean;
import com.hxd.otc.module.convenience.bean.OrderInfoBean;
import com.hxd.otc.module.convenience.bean.PaymentMethodBean;
import com.hxd.otc.module.convenience.bean.PhoneLocationBean;
import com.hxd.otc.module.convenience.bean.RateBean;
import com.hxd.otc.module.convenience.bean.RefuelingServiceBean;
import com.hxd.otc.module.convenience.bean.WaterCoalBillBean;
import com.hxd.otc.module.my.bean.BaseListBean;
import com.hxd.otc.net.BaseObserver;
import com.hxd.otc.net.RetrofitManager;
import com.uber.autodispose.AutoDisposeConverter;

import java.util.ArrayList;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by lzy on 2018/12/12 0012.
 * description ${便民服务Model}
 */
public class ConvenienceModel {
    private ConvenienceModel() {
    }

    private static class SingletonHolder {
        private static final ConvenienceModel instance = new ConvenienceModel();
    }

    public static ConvenienceModel getInstance() {
        return ConvenienceModel.SingletonHolder.instance;
    }

    /**
     * 获取付款方式列表
     */
    public void getPaymentMethodList(@NonNull BaseObserver<ArrayList<PaymentMethodBean>> observer,
                                     AutoDisposeConverter<BaseBean<ArrayList<PaymentMethodBean>>> converter) {
        getConvenienceApi().getPaymentMethodList()
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter).subscribe(observer);
    }

    /**
     * 查询归属地
     */
    public void getPhoneLocation(String phoneNumber, @NonNull BaseObserver<PhoneLocationBean> observer,
                                 AutoDisposeConverter<BaseBean<PhoneLocationBean>> converter) {
        getConvenienceApi().getPhoneLocation(phoneNumber)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter).subscribe(observer);
    }

    /**
     * 获取商品列表
     */
    public void getGoodsList(String moduleId, @NonNull BaseObserver<ArrayList<NorGoodsBean>> observer,
                             AutoDisposeConverter<BaseBean<ArrayList<NorGoodsBean>>> converter) {
        getConvenienceApi().getGoodsList(moduleId)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter).subscribe(observer);
    }

    /**
     * 板块列表
     *
     * @param parentId 0:便民服务列表,其他根据具体的parentId
     */
    public void getModuleList(String parentId, @NonNull BaseObserver<ArrayList<RefuelingServiceBean>> observer,
                              AutoDisposeConverter<BaseBean<ArrayList<RefuelingServiceBean>>> converter) {
        getConvenienceApi().getModuleList(parentId)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter).subscribe(observer);
    }

    /**
     * 获取Banner集合
     *
     * @param positionType
     */
    public void getBannerList(int positionType, @NonNull BaseObserver<ArrayList<ConvenienceBannerBean>> observer,
                              AutoDisposeConverter<BaseBean<ArrayList<ConvenienceBannerBean>>> converter) {
        getConvenienceApi().getBannerList(positionType)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter).subscribe(observer);
    }


    public void getLocation(String province, String city, String rechargeType,
                            @NonNull BaseObserver<ArrayList<ChooseHydropowerCityBean>> observer,
                            AutoDisposeConverter<BaseBean<ArrayList<ChooseHydropowerCityBean>>> converter) {
        getConvenienceApi().getLocation(province, city, rechargeType)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter).subscribe(observer);
    }

    public void getArrearage(String account, String itemId, @NonNull BaseObserver<ArrayList<WaterCoalBillBean>> observer,
                             AutoDisposeConverter<BaseBean<ArrayList<WaterCoalBillBean>>> converter) {
        getConvenienceApi().getArrearage(account, itemId)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter).subscribe(observer);
    }

    public void getHistoryList(String flagType, @NonNull BaseObserver<ArrayList<ConvenienveHistoryBean>> observer,
                               AutoDisposeConverter<BaseBean<ArrayList<ConvenienveHistoryBean>>> converter) {
        getConvenienceApi().getHistoryList(flagType)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter).subscribe(observer);
    }

    public void getHydropowerHistoryList(String businessType, @NonNull BaseObserver<ArrayList<ConvenienveHistoryBean>> observer,
                                         AutoDisposeConverter<BaseBean<ArrayList<ConvenienveHistoryBean>>> converter) {
        getConvenienceApi().getHydropowerHistoryList(businessType)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter).subscribe(observer);
    }

    public void deleteHistory(String id, @NonNull BaseObserver observer,
                              AutoDisposeConverter<BaseBean> converter) {
        getConvenienceApi().deleteHistory(id)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter).subscribe(observer);
    }

    public void deleteFlagType(String flagType, @NonNull BaseObserver observer,
                               AutoDisposeConverter<BaseBean> converter) {
        getConvenienceApi().deleteFlagType(flagType)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter).subscribe(observer);
    }

    public void getOrderList(int showCount, int currentPage, @NonNull BaseObserver<BaseListBean<ConvenienceOrderBean>> observer,
                             AutoDisposeConverter<BaseBean<BaseListBean<ConvenienceOrderBean>>> converter) {
        getConvenienceApi().getOrderList(showCount, currentPage)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter).subscribe(observer);
    }

    public void getOrderDetail(String orderId, @NonNull BaseObserver<ConvenienceOrderBean> observer,
                               AutoDisposeConverter<BaseBean<ConvenienceOrderBean>> converter) {
        getConvenienceApi().getOrderDetail(orderId)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter).subscribe(observer);
    }

    /**
     * @param orderInfoBean 订单信息
     */
    public void payOrder(OrderInfoBean orderInfoBean, @NonNull BaseObserver<OrderFinishedBean> observer,
                         AutoDisposeConverter<BaseBean<OrderFinishedBean>> converter) {
        if (orderInfoBean == null) return;
        if (orderInfoBean.getBusinessType() == null) throw new RuntimeException("请输入businessType");
        getConvenienceApi().payOrder(orderInfoBean.getGoodsId(), orderInfoBean.getModuleId(),
                orderInfoBean.getItemId(), orderInfoBean.getRealSymbol(), orderInfoBean.getRechargeAccount(),
                orderInfoBean.getItemNum(), orderInfoBean.getGasCardTel(), orderInfoBean.getGasCardName(),
                orderInfoBean.getBillCycle(), orderInfoBean.getContractNo(), orderInfoBean.getBusinessType().getValue(),
                orderInfoBean.getPriceUnit(), orderInfoBean.getRemark(), orderInfoBean.getAddress(), orderInfoBean.getCode())
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter).subscribe(observer);
    }

    /**
     * 根据法币价格，获取指定币种需要支付的汇率价格
     *
     * @param sellPrice 法币价格
     * @param priceUint 法币单位
     * @param symbol    法币符号
     */
    public void getRate(String sellPrice, String priceUint, String symbol, @NonNull BaseObserver<RateBean> observer,
                        AutoDisposeConverter<BaseBean<RateBean>> converter) {
        getConvenienceApi().getRate(sellPrice, priceUint, symbol)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter).subscribe(observer);
    }

    /**
     * 修改banner点击量
     *
     * @param bannerId
     */
    public void bannerUpdate(String bannerId, @NonNull BaseObserver<Object> observer,
                             AutoDisposeConverter<BaseBean<Object>> converter) {
        getConvenienceApi().bannerUpdate(bannerId)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter).subscribe(observer);
    }


    private IConvenienceApi getConvenienceApi() {
        return RetrofitManager.getDefault().create(IConvenienceApi.class);
    }
}
